<template>
      <h2>用户借款页面</h2>
      <table>
          <tr>
            <td>借款款项名称</td>
            <td>
                {{info.B_Name}}
            </td>
          </tr>
          <tr>
            <td>最高金额</td>
            <td>
                {{info.BM_Money}}
            </td>
          </tr>
          <tr>
            <td>利息</td>
            <td>
                {{info.BMI_Interest}}
            </td>
          </tr>
          <tr>
            <td>借款金额</td>
            <td>
               <input type="text" v-model="UB_Money" @change="GetMoney"/>
            </td>
          </tr>
          <tr>
            <td>每月还款金额</td>
            <td>
                {{Upayment.toFixed(2)}}
            </td>
          </tr>
          <tr>
            <td></td>
            <td>
               <input type="button" value="确认借款" @click="SaveData">
            </td>
          </tr>
      </table>
</template>

<script setup lang="ts">

import { ref,onMounted} from 'vue';

import axios from 'axios';

import {useRoute} from 'vue-router'

const route = useRoute();

import {useUserStore} from '../../Store/Index.ts'

//实例化仓库
const useStore = useUserStore();

//创建借款信息响应式数据
const info = ref({
    B_Name:"",
    BM_Money:0,
    BMI_Interest:0,
    B_Year:0
})

const UB_Money =ref("")

const Upayment = ref(0)

//获取借款金额
const GetMoney =()=>{
    axios({
       url:"https://localhost:44337/api/Borrow/GetUserPayment",
       method:"get",
       params:{
        UBmoney:UB_Money.value,
        BMI_Interest:info.value.BMI_Interest,
        Byear:info.value.B_Year
       }
    }).then(res=>{
        console.log(res.data);
        Upayment.value=res.data;
       }
    ).catch(err=>{
        console.log(err);
    })
}

onMounted(()=>{
     LoadData();
})

const LoadData =() =>{
    axios({
        url:"https://localhost:44337/api/Borrow/GetUserBorrowInfo",
        method:"get",
        params:{
            Bid:route.params.Bid
        }
    }).then(res=>{
        console.log(res.data);
        info.value=res.data[0];
    }).catch(err=>{
        console.log(err);
    })
}

//完成用户借款
const SaveData =()=>{
    axios({
        url:"https://localhost:44337/api/Borrow/AddUserBorrowMoney",
        method:"post",
        data:{
            U_Id:useStore.Uid,
            B_id:route.params.Bid,
            UBM_Money:UB_Money.value
        }
    }).then(res=>{
        if(res.data>0)
        {
            alert('借款成功')
            
        }
        else
        {
            alert('借款失败')
        }
    }).catch(err=>{
        console.log(err);
    })
}

</script>